//
//  HelpViewController.m
//  SafeFoodManagerDemo
//
//  Created by ios-1 on 2018/3/13.
//  Copyright © 2018年 bob. All rights reserved.
//

#import "HelpViewController.h"
#import "PatrolTableViewCell.h"
#import "SubjectListTableViewCell.h"
#import "SendPatrolViewController.h"
#import "AddRecruitViewController.h"
#import "HelpListViewController.h"
#import "HelpModel.h"
@interface HelpViewController ()

@end

@implementation HelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"帮助中心";
    self.homeTableView.estimatedRowHeight=70;
    
    [self.homeTableView registerNib:[UINib nibWithNibName:@"SubjectListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.homeTableView.tableHeaderView = [self tabHeadView];
    
    
    [self.homeTableView.mj_header beginRefreshing];
}

- (void)creatData
{
    HelpData *data = [HelpData new];
    data.name = @"常用食品添加剂查询";
    [self.dataArr addObject:data];
    
    [self.homeTableView reloadData];
}
- (UIView*)tabHeadView
{
    UIView *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    headview.backgroundColor = [UIColor clearColor];
    
    return headview;
}
#pragma mark-tablevie代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HelpData *data=self.dataArr[indexPath.row];
    
    SubjectListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.SubjectNameLabel.text = data.name;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HelpData *data=self.dataArr[indexPath.row];
    
    HelpListViewController *Patro=[HelpListViewController new];
    JobListModel *list=[JobListModel new];
    list.pageOn = YES;
    list.pageSize = 50;
    list.currentPage = 1;
    list.ptype = @"1";
    
    Patro.ishidenSearch = NO;
    Patro.Reqmodel = list;
    Patro.isEdit= NO;
    Patro.isdeleMore=NO;
    Patro.isGroupStyle=YES;
    Patro.searchName=@"请输入添加剂名称";
    Patro.title=data.name;
    [self.navigationController pushViewController:Patro animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    
    view.tintColor=TBColorTableViewBg;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

#pragma mark-空白页点击事件
- (void)actionTapEmptyDataSetNodataView
{
    [self.homeTableView.mj_header beginRefreshing];
    
}


/**
 *  MJ头部刷新(封装层方法, 需要子类继承或者重写)
 */
- (void)actionMJHeaderRefresh
{
    self.Reqmodel.currentPage=1;
    
    [self.homeTableView.mj_footer resetNoMoreData];
    
    [self loadDatas];
}

/**
 *  MJ底部加载(封装方法, 需要子类继承或者重写)
 */
- (void)actionMJFooterLoadMore
{
    self.Reqmodel.currentPage++;
    
    [self loadDatas];
}


-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/additiveCatList.json" andrequestTime:5 andParams:self.Reqmodel.mj_keyValues andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        HelpModel *InforModel=[HelpModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            [self.dataArr removeAllObjects];
            
            [self creatData];
            
            [self NetRequestSuccess:self scrollView:self.homeTableView];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            if (self.Reqmodel.currentPage>1) {
                
                self.Reqmodel.currentPage=self.Reqmodel.currentPage-1;
            }
            
            [self NetRequestFail:self scrollView:self.homeTableView msg:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
